package wiki.douzi.helpyougrow.service.impl;

import wiki.douzi.helpyougrow.domain.Admin;
import wiki.douzi.helpyougrow.domain.User;
import wiki.douzi.helpyougrow.mapper.AdminMapper;
import wiki.douzi.helpyougrow.service.AdminService;
import org.springframework.stereotype.Service;

import java.util.List;
import javax.annotation.Resource;

/**
 * (Admin)表服务实现类
 *
 * @author Douzi
 * @since 2022-06-18 16:39:19
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */
    @Override
    public List<Admin> queryAll() {
        return this.adminMapper.queryAll();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(Integer id) {
        return this.adminMapper.queryById(id);
    }

    @Override
    public Admin login(String username, String password) {
        Admin admin = this.adminMapper.queryByName(username);
        if (admin != null) {
            if (admin.getPassword().equals(password)){
                return admin;
            }
        }
        return null;
    }

    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin insert(Admin admin) {
        this.adminMapper.insert(admin);
        return admin;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin update(Admin admin) {
        this.adminMapper.update(admin);
        return this.queryById(admin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.adminMapper.deleteById(id) > 0;
    }
}

